<template>
  <div class="login-container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <!-- 登录表单 -->
        <el-form
          class="login-form"
          :model="loginForm"
          :rules="guize"
          ref="loginForms"
        >
          <h1>Hello,你好！</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              type="password"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              type="primary"
              size=""
              class="login_btn"
              @click="login()"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { ElNotification } from 'element-plus'
// 引入路由器
import { useRouter, useRoute } from 'vue-router'
// 引入用户数据仓库
import userMessageStore from '@/store/modules/user'
// 引入判断当前是什么时间段的函数
import { getTime } from '@/utils/time'

// 收集用户登录账号和密码
let loginForm = reactive({ username: 'admin', password: '111111' })
// 登录加载动画
let loading = ref(false)
// 获取路由器
let $router = useRouter()
let $route = useRoute()
// 获取用户相关的仓库
let useStore = userMessageStore()

// 获取el-form表单实例
let loginForms = ref()

// 方法
// 进行登录
async function login() {
  // 等待表单完成验证
  await loginForms.value.validate()
  // 登录按钮加载动画 --开
  loading.value = true
  try {
    await useStore.userLogin(loginForm)
    let redirect: any = $route.query.redirect
    // 编程时导航跳转到首页
    $router.push({ path: redirect || '/' })
    // 登录成功提示信息
    ElNotification({
      type: 'success',
      message: '登陆成功! 欢迎回来',
      title: `HI! ${getTime()}好`,
    })
    // 登录按钮加载动画 --关
    loading.value = false
  } catch (error) {
    //登录失败的提示信息
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
    loading.value = false
  }
}

//自定义校验规则函数
const validatorUserName = (rule: any, value: any, callback: any) => {
  //rule:即为校验规则对象
  //value:即为表单元素文本内容
  //函数:如果符合条件callBack放行通过即为
  //如果不符合条件callBack方法,注入错误提示信息
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号长度至少五位'))
  }
}

const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码长度至少六位'))
  }
}

// 表单规则对象
const guize = {
  username: [
    // 默认校验规则
    // { required: true, message: "账号不能为空", trigger: "blur"},
    // { required: true, min: 5, max: 10, message: '账号长度至少六位', trigger: 'change' }
    // 自定义角度规则
    { trigger: 'change', validator: validatorUserName },
  ],
  password: [
    // 默认校验规则
    // { required: true, min: 6, max: 15, message: '密码长度至少6位', trigger: 'change' }
    { trigger: 'change', validator: validatorPassword },
  ],
}
</script>

<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  background-image: url('@/assets/images/background.jpg');
  background-repeat: no-repeat;
  background-size: cover;

  .login-form {
    position: relative;
    width: 70%;
    top: 30vh;
    background-image: url('@/assets/images/login_form.png');
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
